package document;

import java.awt.Color;
import java.util.HashMap;

import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

/**
 * Library for methods used for document keyword highlighting.
 * @author Brittin
 *
 */
public class SyntaxHighlighter {

	/**
	 * Highlights the keywords in the text pane using the dictionary of the document type included.
	 * @param pane pane to update.
	 * @param doc document type to use.
	 */
	public static void highlightDocument(JTextPane pane, AwesomeDocument doc) {
		if (doc == null)
			return;
			
			HashMap<String, Color> dict = doc.getLibrary();
			String text = pane.getText();
			text = text.replaceAll("\r","");
			//text = text.replaceAll("\n"," ");
			//text = text.replaceAll("\n\r"," ");
			//text = text.replaceAll("\r\n"," ");
			//text = text.replaceAll("\b"," ");
			//text = text.replaceAll("\t"," ");
			
			String[] words = text.split("[ \t\r\n\b.]");

			
			
			
			
			int offset = 0;
			StyleContext sc = StyleContext.getDefaultStyleContext();
			AttributeSet aset = null;	
//			aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
//					StyleConstants.Foreground, Color.black);
//			pane.getStyledDocument().setCharacterAttributes(
//					beginindex, endindex-beginindex, aset, false);
			
			
			for (int i = 0; i < words.length; i++) {
				int beginindex=0;
				int endindex =0;
				if (!(words[i].trim().length() == 0) || !words[i].equals("\b") || !words[i].equals("") || !words[i].equals(" ") || !words[i].equals("\t") 
						|| !words[i].equals("\r\n") ||!words[i].equals("\n") || !words[i].contains("\r")) {
					
					//Account for words surrounded by parenthesis 
					Color color = dict.get(words[i]);

					beginindex = text.indexOf(words[i], offset);
					
					endindex = beginindex + words[i].length();
					
					
					if (color != null) { // color the word
						//System.out.println("\n COLOR: " + "\"" + words[i]+ "\" " +
							//	beginindex + " " + endindex + " " +(offset+words[i].length()));
						aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
								StyleConstants.Foreground, color);
					} else { // make it standard black
						//System.out.println("\n BLACK: " + "\"" + words[i] + "\" " + (offset+words[i].length()));
						aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
								StyleConstants.Foreground, Color.black);
					}

					// Account for braces
					if (!words[i].equals("}") || !words[i].equals("{")) {

						pane.getStyledDocument().setCharacterAttributes(
								beginindex, endindex-beginindex, aset, false);
					}
				}
				//if (words[i].length() == 0)
					//offset++;
				//offset += words[i].length();
				offset = endindex;
			}
	}
	
	private String formatWord(String originalWord) {
		String word = originalWord;
		word = word.replace("(", "");
		word = word.replace(")", "");
		word = word.replace("[", "");
		word = word.replace("]", "");
		word = word.replace("{", "");
		word = word.replace("}", "");
		return word;
	}
}
